package com.aaa.yq127.dao.impl;



import com.aaa.yq127.dao.StaffDao;
import com.aaa.yq127.entity.Staff;
import com.aaa.yq127.util.JDBCUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class StaffDaoImpl implements StaffDao {
    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Map<String, Object>> selStaff() {
        String sql = "select * from t_staff";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql);
        if (maps.size() >0){
            return maps;
        }
        return null;
    }

    /**
     * 按照id删除
     * @param id
     * @return
     */
    @Override
    public int delStaff(Integer id) {
        String sql = "delete from t_staff where id =?";
        int i = JDBCUtil.update(sql, id);
        return i;
    }

    /**
     * 按照id查询
     * @param id
     * @return
     */
    @Override
    public Staff getOneStaff(Integer id) {
        String sql="select * from t_staff where id =?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, id);
        if (maps.size()>0){
            Map<String, Object> map = maps.get(0);
            Staff staff = new Staff((Integer) map.get("id"), (Integer) map.get("staffId"), (String) map.get("staffName"), (String) map.get("password"), (String) map.get("phone"), (String) map.get("idCard"), (String) map.get("address"), (Date) map.get("createdTime"), (Date) map.get("resignedTime"), (Integer) map.get("status"), (Integer) map.get("roleId"), (Integer) map.get("deptid"), (String) map.get("url"));
            return staff;
        }
        return null;
    }

    /**
     * 添加一条数据
     * @param staff
     * @return
     */
    @Override
    public int insStaff(Staff staff) {
        String sql="insert into t_staff values(null,?,?,?,?,?,?,?,?,?,?)";
        int i = JDBCUtil.update(sql, staff.getStaffId(), staff.getStaffName(), staff.getPhone(), staff.getIdCard(), staff.getAddress(), staff.getCreatedTime(), staff.getStatus(), staff.getRoleId(), staff.getDeptid());
        return i;
    }

    @Override
    public int updStaff(Staff staff) {
        String sql ="update t_staff set staffName =?,phone=?,idCard=?,address=?,roleId=?,deptid=? where id=?";
        int i = JDBCUtil.update(sql, staff.getStaffName(), staff.getPhone(), staff.getIdCard(), staff.getAddress(), staff.getRoleId(), staff.getDeptid(), staff.getId());
        return i;
    }

}
